import requests
import semver
from PyQt5.QtWidgets import QMessageBox

class UpdateChecker:
    GITHUB_API = "https://api.github.com/repos/yourname/csdn-downloader/releases/latest"

    def __init__(self, current_version):
        self.current = semver.VersionInfo.parse(current_version)
        
    def check(self):
        try:
            resp = requests.get(self.GITHUB_API, timeout=5)
            latest = resp.json()['tag_name']
            return semver.compare(latest, self.current) > 0
        except Exception as e:
            print(f"更新检查失败: {str(e)}")
            return False

    def show_update_dialog(self, parent):
        msg = QMessageBox(parent)
        msg.setIcon(QMessageBox.Information)
        msg.setText("发现新版本！")
        msg.setInformativeText("是否立即下载更新？")
        msg.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
        return msg.exec_()